<?php
 $objRecord;
class record {

    public $namaTabel = '';
    public $strQuery = '';

    function __construct() {
        if (file_exists('../koneksi/koneksi.php')) {
            include_once '../koneksi/koneksi.php';
            include_once '../kelas/amankanParam.php';
            include_once '../kelas/fungsi.php';
        } else if (file_exists('koneksi/koneksi.php')) {
            include_once 'koneksi/koneksi.php';
            include_once 'kelas/fungsi.php';
            include_once 'kelas/amankanParam.php';
        };
    }

    function getWhereIn($retField,$field,$strIn){
        $str = "SELECT ".$retField." FROM ".$this->namaTabel." WHERE ".$field." IN (".$strIn.")";
        $this->query($str);
        $ex = $this->execute();
        $retStr = '';
        if(($ex['success']==true)&&($ex['total']>0)){
            foreach ($ex['result'] as $value) {
                $retStr.=$value[$retField].', ';
            }
        };
        return $retStr;
    }
    function update($arrSet, $arrCond, $limit = null, $start = null) {
        $s = "UPDATE " . $this->namaTabel;
        foreach ($arrSet as $key => $value) {
            $set[] = $key . "`='" . $value;
        }
        $strSet = "`" . implode("',`", $set) . "'";
        $s .= " SET " . $strSet;
        if (is_array($arrCond) && count($arrCond) > 0) {
            foreach ($arrCond as $key => $value) {
                $cond[] = $key . "` = '" . $value;
            };
            $strWh = "`" . implode("' AND `", $cond) . "'";
            $s .= " WHERE " . $strWh;
        }
        $s .= $this->limit($limit, $start);
        $this->strQuery = $s;
    }

    function delete($arrCondition, $limit = null, $start = null) {
        $s = "DELETE FROM " . $this->namaTabel;
        if (is_array($arrCondition) && count($arrCondition) > 0) {
            foreach ($arrCondition as $key => $value) {
                $cond[] = $key . "` = '" . $value;
            };
            $strWh = "`" . implode("'  AND   `", $cond) . "'";
            $s .= " WHERE " . $strWh;
        }
        $s .= $this->limit($limit, $start);
        $this->strQuery = $s;
    }

    function create($arrInsert = '') {
        if (is_array($arrInsert) && count($arrInsert) > 0) {
            foreach ($arrInsert as $key => $val) {
                $k[] = $key;
                $v[] = $val;
            };
            $strField = implode('`,`', $k);
            $strVal = implode("','", $v);
            $this->strQuery = "INSERT INTO " . $this->namaTabel . " (`" . $strField . "`) VALUES ('" . $strVal . "')";
        };
    }

    function get($fields = '*', $cond = array(), $limit = null, $start = null, 
        $orderBy = null, $orderType = 'ASC', $condStr = null, $arrSearch = array()) {
        $sWh = " WHERE 1=1 ";
        $ord = '';
        $sLimit = '';
        if (isset($orderBy)) {
            $ord .= ' ORDER BY ' . $orderBy . ' ' . $orderType;
        };
        if (isset($limit) && (int) $limit > 0) {
            if (isset($start) && (int) $start > 0) {
                $sLimit = " LIMIT " . $start . ", " . $limit;
            } else {
                $sLimit = " LIMIT " . $limit;
            }
        }
        if (is_array($cond) && count($cond) > 0) {
            foreach ($cond as $keyC => $vCond) {
                $sW[] = $keyC . "` = '" . $vCond;
            };
            $sWh .=" AND `" . implode("' AND `", $sW) . "'";
        };
        if (is_array($arrSearch) && count($arrSearch) > 0) {
            $sWh .= " AND (";
            foreach ($arrSearch as $key => $value) {
                $arSea[] = $key . " LIKE '%" . $value . "%'";
            };
            $sWh .= implode(" OR ", $arSea);
            $sWh .= " ) ";
        }
        if (isset($condStr) && strlen($condStr) > 0) {
            $sWh .= " AND " . $condStr;
        }
        $str = "SELECT " . $fields . " FROM " . $this->namaTabel . $sWh . $ord . $sLimit;
        $this->strQuery = $str;
    }

    function query($str) {
        $this->strQuery = $str;
    }

    function num_rows($arrCond = null, $searchCond = array(), $strWhere = '') {
        $s = "SELECT COUNT(*) AS total FROM " . $this->namaTabel;
        $wh = " WHERE 1=1 ";
        if (is_array($arrCond) && count($arrCond) > 0) {
            $wj = array();
            foreach ($arrCond as $key => $value) {
                $wj[] = $key . "`='" . $value;
            }
            $wh .=" AND `" . implode("' AND `", $wj) . "'";
        };
        if (is_array($searchCond) && count($searchCond) > 0) {
            $wh .= " AND (";
            $arWhere = array();
            foreach ($searchCond as $key => $value) {
                $arWhere[] = $key . " LIKE '%" . $value . "%'";
            };
            $wh .= implode(' OR ', $arWhere);
            $wh .= " ) ";
        }

            if(strlen($strWhere)>0){
                $wh .= " AND ".$strWhere;
            }
        $this->strQuery = $s . $wh;
    }

    function execute() {
        $q = mysql_query($this->strQuery)or die('Error: '.mysql_error());
        if ($q == false) {
            $arrRet = array('success' => false,
                'error_num' => mysql_errno(),
                'error_msg' => mysql_error(),
                'error_query' => $this->strQuery);
        } else {
            $arrRet = array('success' => true);
            if (is_resource($q)) {
                $arrDt = array();
                while ($dt = mysql_fetch_array($q)) {
                    $arrDt[] = $dt;
                };
                $arrRet['result'] = $arrDt;
                $arrRet['total'] = mysql_num_rows($q);
            }
        }
        return $arrRet;
    }

    private function limit($limit = null, $start = null) {
        $s = '';
        if (isset($limit) && $limit > 0) {
            if (isset($start)) {
                $s .= " LIMIT " . $limit . " ," . (int) $start;
            } else {
                $s .=" LIMIT " . $limit;
            }
        };
        return $s;
    }

};

$objRecord = new record();